import re
import allure
import logging


# 单独写一个方法/函数来处理正则表达式
def re_match_value(string, pattern, index=0):
    """
    正则表达式提取值
    :param string: 字符串
    :param pattern: 正则表达式
    :param index: 获取第几个值，默认是0
    :return:
    """
    matches = [match.group() for match in re.finditer(pattern, string)]
    return matches[index]


def re_extractor(case, all_res):
    if case.get("reExData"):
        with allure.step("4.正则提取"):
            for key, value in eval(case["reExData"]).items():
                # value:表达式
                v = re_match_value(str(all_res), value)
                all_res[key] = v
        logging.info(f"4.正则提取,根据case[reExData]提取数据,此时全局变量为:{all_res}")